import sys
import pygame
import hero
import heroOther
import attack
import attack2
from pygame.locals import *

pygame.init()
screen_size = width, height = 1200,660
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("battleGame")
bg = pygame.image.load("bg.jpg").convert_alpha()
hero1 = hero.ASN(screen_size)
hero2 = heroOther.APC(screen_size)
heroGroup1 = pygame.sprite.Group()
heroGroup1.add(hero1)
heroGroup2 = pygame.sprite.Group()
heroGroup2.add(hero2)

sword = attack.ATTACK(hero1.rect.topright,screen_size)
swordGroup = pygame.sprite.Group()
swordGroup.add(sword)
sword2 = attack.ATTACK(hero2.rect.topright,screen_size)
swordGroup2 = pygame.sprite.Group()
swordGroup2.add(sword2)
dir = 0
dir2 = 0
asn_index = 0
asn_index2 = 0
asn_index3 = 0
apc_index = 0
delay = 100

BLACK = (0,0,0)
RED = (255,0,0)
GREEN = (0,255,0)
clock = pygame.time.Clock()
running = True
while running:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    screen.blit(bg,(0,0))
    #检测按键
    key_pressed = pygame.key.get_pressed()
    #hero1被操控
    if key_pressed[K_w]:
        hero1.moveUp()
    if key_pressed[K_s]:
        hero1.moveDown()
    if key_pressed[K_a]:
        dir = 0
        hero1.moveLeft()
    if key_pressed[K_d]:
        dir = 1
        hero1.moveRight()
    #hero2被操控
    if key_pressed[K_UP]:
        hero2.moveUp()
    if key_pressed[K_DOWN]:
        hero2.moveDown()
    if key_pressed[K_LEFT]:
        dir2 = 0
        hero2.moveLeft()
    if key_pressed[K_RIGHT]:
        dir2 = 1
        hero2.moveRight()
    #显示hero1
    if (key_pressed[K_w] or key_pressed[K_s] \
        or key_pressed[K_a] or key_pressed[K_d]) == 0:
        screen.blit(hero1.idleimgs[0],hero1.rect)
    else:
        if not(delay%9):
            asn_index = (asn_index + 1) % 8
        screen.blit(hero1.idleimgs[asn_index],hero1.rect)

    #显示hero2
    if hero2.active == True:
        if (key_pressed[K_UP] or key_pressed[K_DOWN]\
            or key_pressed[K_LEFT] or key_pressed[K_RIGHT]) == 0:
            screen.blit(hero2.idleimgs[0],hero2.rect)
        else:
            if not(delay%9):
                apc_index = (apc_index + 1) % 8
            screen.blit(hero2.idleimgs[apc_index],hero2.rect)
    #绘制血槽
    pygame.draw.line(screen,BLACK,\
                     (hero2.rect.left, hero2.rect.top - 5),\
                     (hero2.rect.right,hero2.rect.top - 5),\
                     2)
    blood_remain = hero2.blood / 3
    pygame.draw.line(screen,GREEN,\
                     (hero2.rect.left, hero2.rect.top - 5),\
                     (hero2.rect.left+hero2.rect.width*blood_remain,hero2.rect.top - 5),\
                     2)
    if key_pressed[K_f]:
        if dir == 0:
            sword.resetLeft(hero1.rect.midtop)
        else:
            sword.resetRight(hero1.rect.midtop)
    if sword.active == True:
        if dir == 0:
            sword.moveLeft()
        else:
            sword.moveRight()
        screen.blit(sword.imgs[0],sword.rect)

    if key_pressed[K_g]:
        if dir2 == 0:
            sword2.resetLeft(hero2.rect.midtop)
        else:
            sword2.resetRight(hero2.rect.midtop)
    if sword2.active == True:
        if dir2 == 0:
            sword2.moveLeft()
        else:
            sword2.moveRight()
        screen.blit(sword2.imgs[0],sword2.rect)
    #动画切换延时
    if delay > 0:
        delay -= 1
    else:
        delay =  100
    if pygame.sprite.spritecollide(sword,heroGroup2,False,pygame.sprite.collide_mask):
        if hero2.blood == 0:
            hero2.active = False
            hero2.clear()
        else:
            hero2.blood -= 1
        sword.active = False
        sword.clear()
    if pygame.sprite.spritecollide(sword2,heroGroup1,False,pygame.sprite.collide_mask):
        hero1.active = False
        hero1.clear()
        sword2.active = False
    #显示整个窗口
    pygame.display.flip()
    clock.tick(80)



